package com.nuoniu.sibanyun.controller.erp;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nuoniu.sibanyun.common.BaseController;
import com.nuoniu.sibanyun.common.exception.NuoNiuException;
import com.nuoniu.sibanyun.common.lang.StringUtils;
import com.nuoniu.sibanyun.common.result.HttpResult;
import com.nuoniu.sibanyun.entity.erp.ErpInvoiceType;
import com.nuoniu.sibanyun.service.erp.IErpInvoiceTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 发票分类 前端控制器
 * </p>
 *
 * @author 小啊刚
 * @since 2021-02-05
 */
@RestController
@RequestMapping("/erp/invoiceType")
@Api(value = "发票分类", tags = "发票分类")
public class ErpInvoiceTypeController extends BaseController {

    @Autowired
    private IErpInvoiceTypeService invoiceTypeService;

    @ApiOperation("分页获取列表")
    @GetMapping("pageList")
    public HttpResult pageList(Page page, ErpInvoiceType invoiceType) {
        return HttpResult.ok(invoiceTypeService.pageList(page, invoiceType));
    }

    @ApiOperation("保存")
    @PostMapping("save")
    public HttpResult save(@RequestBody ErpInvoiceType invoiceType) {
        if(StringUtils.isBlank(invoiceType.getCode())) {
            throw new NuoNiuException("编码不能为空");
        }
        if(StringUtils.isBlank(invoiceType.getName())) {
            throw new NuoNiuException("名称不能为空");
        }
        if(invoiceType.getType() == null) {
            throw new NuoNiuException("方向不能为空");
        }

        invoiceTypeService.saveInvoiceType(invoiceType);
        return HttpResult.ok();
    }

    @ApiOperation("删除")
    @PostMapping("delete")
    public HttpResult delete(@RequestBody List<Long> ids) {
        if(ids.size() < 1) {
            throw new NuoNiuException("请选择要删除的发票类型");
        }
        invoiceTypeService.deleteByIds(ids);
        return HttpResult.ok();
    }
}
